
/**
 * PROG5 is a basic game of bridge, 4 players are delt 13 randomly shuffled cards.  A method called ITOTAL
 * calculates the points in each 13 card hand, these points are determined by distribution and face cards.
 * Using the SHOW method, the hand is printed out in order based on suit.
 * 
 * Jeff Thomas
 * 10.07.2007
 * CSC 131-2
 * Assignment #5
 */

import java.util.*;

public class PROG5 {
    public static void main(String[] args) {
        int[] cards = new int[52];
        int[] player = new int[13];
        String[] side = {"WEST", "NORTH", "EAST", "SOUTH"};
        cards = shuffle(gen_deck(cards));
        for (int i = 0; i < 4; i++) {
            player = deal13(cards, i + 1);
            SORT(player);
            System.out.println(side[i]);
            SHOW(player, ITOTAL(player, 13));
        }
    }
    
    //Generate the deck of cards
    public static int[] gen_deck(int[] cards) {
        for (int i = 0; i < 52; i++) {
            cards[i] = i + 1;
        }
        return cards;
    }
    
    //Shuffle our deck
    public static int[] shuffle(int[] cards) {
        for (int i = 1; i < 1000; i++) {
            int spot1 = (int) (Math.random() * 52);
            int spot2 = (int) (Math.random() * 52);
            int temp = cards[spot1];
            cards[spot1] = cards[spot2];
            cards[spot2] = temp;
        }
        return cards;
    }
    
    //Deal the cards, 13 to each player
    //Player 1 = West, Player 2 = North, Player 3 = East, Player 4 = South
    public static int[] deal13(int[] cards, int playerno) {
        int[] hand = new int[13];
        //create each hand based off the player number
        if (playerno == 1) {
            for (int i = 0; i <= 12; i++) {
                hand[i] = cards[i];
            }
        }
        else if (playerno == 2) {
            for (int i = 0; i <= 12; i++) {
                hand[i] = cards[i + 13];
            }
        }
        else if (playerno == 3) {
            for (int i = 0; i <= 12; i++) {
                hand[i] = cards[i + 26];
            }
        }
        else {
            for (int i = 0; i <= 12; i++) {
                hand[i] = cards[i + 39];
            }
        }
        return hand;
    }
    
    //Sort our cards
    public static void SORT(int[] hand) {
        Arrays.sort(hand);
    }
    
    //Calculate the total points from each hand
    public static int ITOTAL(int[] hand, int arraylength) {
        //calculate points for face cards
        int totalpoints = 0;
        for (int i = 0; i < arraylength; i++) {
            if (hand[i] % 13 == 0)
                totalpoints = totalpoints + 4;
            else if ((hand[i] + 1) % 13 == 0)
                totalpoints = totalpoints + 3;
            else if ((hand[i] + 2) % 13 == 0) 
                totalpoints = totalpoints + 2;
            else if ((hand[i] + 3) % 13 == 0)
                totalpoints = totalpoints + 1;
        }
        
        //calculate distribution points
        int [] suittotal = {0, 0, 0, 0};
        for (int i = 0; i < arraylength; i++) {
            if (hand[i] <= 13)
                suittotal[0] += 1;
            else if (hand[i] <= 26)
                suittotal[1] += 1;
            else if (hand[i] <= 39)
                suittotal[2] += 1;
            else
                suittotal[3] += 1;
        }
        for (int j = 0; j <= 3; j++) {
            if (suittotal[j] == 0)
                totalpoints = totalpoints + 3;
            else if (suittotal[j] == 1)
                totalpoints = totalpoints + 2;
            else if (suittotal[j] == 2)
                totalpoints = totalpoints + 1;
        }
        return totalpoints;
    }
    
    //SHOW displays the contents of each the hand and the total points
    public static void SHOW(int[] hand, int points) {
        String[] suit = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
        String[] cardvalue = {"2","3","4","5","6","7","8","9","10","J","Q","K","A","2","3","4","5","6","7","8","9","10","J","Q","K","A","2","3","4","5","6","7","8","9","10","J","Q","K","A","2","3","4","5","6","7","8","9","10","J","Q","K","A",};
    int n = 0;
        for (int i = 0; i < hand.length - 1; i++) {
            //the if statments here print out the suit if it has not already been printed
            if (n == 0 && hand[i] < 13) {
                System.out.println("    " + suit[0]);
                n += 1;
            }
            if (n == 1 && hand[i] > 13) {
                System.out.println("    " + suit[1]);
                n += 1;
            }
            if (n == 2 && hand[i] > 26) {
                System.out.println("    " + suit[2]);
                n += 1;
            }
            if (n == 3 && hand[i] > 39) {
                System.out.println("    " + suit[3]);
                n += 1;
            }
            System.out.println("    " + cardvalue[hand[i]]);
        }
        System.out.println("NUMBER OF POINTS " + points);
        System.out.println();
    }
}
